package com.lottery.utils.robotThread;

import com.lottery.dao.qiuqiu.robotDao.RobotBetamountMapper;
import com.lottery.dao.qiuqiu.robotDao.RobotInfoMapper;
import com.lottery.entity.RobotBetamount;
import com.lottery.entity.RobotInfo;
import com.lottery.initData.InitData;
import com.lottery.service.qiuqiu.MsgService.MsgNoticeService;
import com.lottery.utils.DateUtil;
import com.lottery.utils.RandCodeUtil;
import com.lottery.utils.lobbyUtils.impl.BjscPlayUtilImpl;
import com.lottery.utils.lobbyUtils.impl.SscPlayutilImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class BJSCRobotThread implements Runnable {


    @Resource
    private RobotInfoMapper robotInfoDao;

    @Resource
    private BjscPlayUtilImpl bjscPlayutil;

    @Resource
    private MsgNoticeService msgServie;

    @Resource
    private RobotBetamountMapper robotDao;

    Logger logger = LoggerFactory.getLogger(this.getClass());



    public int agencyMark;
    @Override
    public void run() {
        List<RobotInfo> robotInfos = robotInfoDao.searchRobotListInfo(agencyMark);
        logger.debug("BJSCRobotThread " + agencyMark + " start");
        RobotBetamount ro = robotDao.searchByAgency(agencyMark);
        Integer moneyMax = ro.getMoneyMax();
        Integer moneyMin = ro.getMoneyMin();
        Integer robotActiveMax = ro.getRobotActiveMax();
        Integer robotActiveMin = ro.getRobotActiveMin();
        Integer baseOnline = ro.getBaseOnline();
        int robotBetOnline = RandCodeUtil.getRandNum(robotActiveMin, robotActiveMax);

        //获取渠道商正在使用的玩法下注信息
        List<Integer> playwayIdList = new ArrayList<>();
        InitData.BJSC_ODD_MAP.get(agencyMark + "").forEach((k, v) -> {
            playwayIdList.add(k);
        });
        List<String> betRuleList = new ArrayList<>();
        InitData.BJSC_BETRULE_REF_PLAYWAYID.forEach((k, v) -> {
            if (playwayIdList.contains(v)) {
                betRuleList.add(k);
            }
        });
        while (true) {
            logger.debug(Thread.currentThread().getName()+"BJSCRobotThread " + agencyMark + " start");
            RobotBetamount ro1 = robotDao.searchByAgency(agencyMark);
            if (ro1.getRobotSwitch() == 0) {
                int hour = Integer.parseInt(DateUtil.getFormatDate("HH"));
                if (hour > 2 && hour < 8) { // 这个时段机器人都下线
                    InitData.ROBOT_ONLINE_NUM.put(agencyMark + "", 0);
                    try {
                        Thread.sleep(6 * 60 * 60 * 1000);
                    } catch (InterruptedException e) { // TODO Auto-generated catch block
                        logger.error("BjscRobotThread " + agencyMark + " error ,{}", e);
                    }
                } else {
                    //下注时间判断
                    boolean timeJudge = bjscPlayutil.judgeBetTime();
                    int onlineNum = RandCodeUtil.getRandNum(robotActiveMin, robotActiveMax) + baseOnline;
                    InitData.ROBOT_ONLINE_NUM.put(agencyMark + "", onlineNum);
                    if (timeJudge) {
                        if (robotBetOnline > robotInfos.size()) {
                            robotBetOnline = robotInfos.size();
                        }
                        RobotInfo rz = robotInfos.get(RandCodeUtil.getRandNum(0, robotBetOnline - 1));
                        String bet = betRuleList.get(RandCodeUtil.getRandNum(0, betRuleList.size() - 1));
                        int money = (moneyMin + RandCodeUtil.getRandNum(0, moneyMax - moneyMin)) / 10;
                        String betRule = bet + "/" + money * 10;
                        msgServie.RobotinsertBetNotice(rz, betRule, 3, agencyMark + "");
                        //获取下注间隔时间
                        long time = 4 * 60 * 1000 / robotBetOnline;
                        try {
                            Thread.sleep(time * RandCodeUtil.getRandNum(8, 12) / 10);
                        } catch (InterruptedException e) {
                            logger.error("BjscRobotThread sleep Error ,{}", e);
                        }
                    } else {
                        robotBetOnline = RandCodeUtil.getRandNum(robotActiveMin, robotActiveMax);
                        try {
                            Thread.sleep(50 * 1000);
                        } catch (InterruptedException e) {
                            logger.error("BjscRobotThread sleep Error ,{}", e);
                        }
                    }
                }
            } else {
                InitData.ROBOT_ONLINE_NUM.put(agencyMark + "", 0);
                break;
            }
        }
    }
}
